package com.decent.manager.system.auth.vo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.decent.common.entity.manager.SystemRole;
import com.decent.common.entity.manager.SystemUser;
import com.decent.common.enums.CommonStatusEnum;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 登录用户模型
 *
 * @className: LoginUser
 * @author: wangyan
 * @date: 2022/4/16
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@SuperBuilder(toBuilder = true)
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({"password", "username", "credentialsNonExpired", "authorities", "accountNonLocked", "accountNonExpired"})
public class LoginUser extends SystemUser implements UserDetails {
    private static final long serialVersionUID = 6370591049389896815L;
    /**
     * 角色信息
     */
    private SystemRole role;

    /**
     * 权限信息
     */
    private Set<String> permissions;

    /**
     * 登录菜单信息，AntDesign版本菜单
     */
    private List<LoginMenuTreeNode> menus;

    /**
     * 角色名称集合
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<UserAuthority> grantedAuthorities = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotEmpty(role)) {
            String roleName = role.getRoleName();
            UserAuthority authority = new UserAuthority(roleName);
            grantedAuthorities.add(authority);
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return super.getLoginPassword();
    }

    @Override
    public String getUsername() {
        return super.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return CommonStatusEnum.NORMAL.equals(super.getUserStatus());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return CommonStatusEnum.NORMAL.equals(super.getUserStatus());
    }
}
